package impls.classes;

import java.util.Random;

/**
 * class to save and move position
 * 
 * @author thailow
 * 
 */
public class Position {

	private int x;

	private int y;

	public Position() {
		Random r = new Random();
		x = r.nextInt(Constants.MAXCOORD);
		y = r.nextInt(Constants.MAXCOORD);
	}

	public int getX() {
		return x;
	}

	public int getY() {
		return y;
	}

	public Position(int x, int y) {
		this.x = x;
		this.y = y;
	}

	public void setX(int x) {
		this.x = x;
	}

	public void setY(int y) {
		this.y = y;
	}

	/**
	 * changes position depending on given direction
	 * 
	 * @param dir
	 * @return
	 */
	public String move(int dir) {
		switch (dir) {
		case Constants.UP:
			y++;
			y = y % Constants.MAXCOORD;
			return "up";
		case Constants.RIGHT:
			x++;
			x = x % Constants.MAXCOORD;
			return "right";
		case Constants.DOWN:
			y = y == Constants.MINCOORD ? Constants.MAXCOORD - 1 : y - 1;
			return "down";
		case Constants.LEFT:
			x = x == Constants.MINCOORD ? Constants.MAXCOORD - 1 : x - 1;
			return "left";
		default:
			return "";
		}
	}

	/**
	 * calculates minimum direction from this position to given pos
	 * 
	 * @param pos
	 * @return
	 */
	public int minDist(Position pos) {
		int distX;
		int distY;
		int temp;

		distX = Math.abs(x - pos.getX());
		temp = Math.abs(distX - Constants.MAXCOORD);
		distX = temp < distX ? temp : distX;

		distY = Math.abs(y - pos.getY());
		temp = Math.abs(distY - Constants.MAXCOORD);
		distY = temp < distY ? temp : distY;

		return distX + distY;
	}

	/**
	 * calculates distance from start to dest (one dimensional)
	 * 
	 * @param start
	 * @param dest
	 * @return
	 */
	public int dist(int start, int dest) {
		int dist;
		int temp;

		dist = dest - start;
		temp = dist < 0 ? Constants.MAXCOORD + dist : dist - Constants.MAXCOORD;
		dist = Math.abs(temp) < Math.abs(dist) ? temp : dist;

		return dist;
	}

	/**
	 * prints out position as string
	 */
	public String toString() {
		return "Position: x = " + x + ", y = " + y;
	}
}
